package com.woniuxy.hrm.service.impl;


import com.alibaba.fastjson.JSON;
import com.woniuxy.hrm.entity.Good;
import com.woniuxy.hrm.entity.Order;
import com.woniuxy.hrm.entity.OrderItem;
import com.woniuxy.hrm.mapper.GoodMapper;
import com.woniuxy.hrm.mapper.OrderItemMapper;
import com.woniuxy.hrm.mapper.OrderMapper;
import com.woniuxy.hrm.producer.AddProducer;
import com.woniuxy.hrm.producer.AddProducer1;
import com.woniuxy.hrm.producer.DelayProducer;
import com.woniuxy.hrm.service.GoodService;
import com.woniuxy.hrm.util.ResponseData;
import com.woniuxy.hrm.util.ResponseEnum;
import com.woniuxy.hrm.util.SnowflakeIdGenerator;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@AllArgsConstructor
public class GoodServiceImpl implements GoodService {
    private GoodMapper goodMapper;
    private OrderMapper orderMapper;
    private OrderItemMapper orderItemMapper;
    private DelayProducer  delayProducer;
    private AddProducer addProducer;
    private AddProducer1 addProducer1;
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResponseData<?> queryAllGood() {
        return ResponseData.ok(goodMapper.queryAllGood()) ;
    }

    @SneakyThrows
    @Override
    public ResponseData<?> addOrder(List<Good> goodList) {

//        List<Good> goods = new ArrayList<>();
//        for (Good good : goodList) {
//            if (good.getNumber()!=0){
//                goods.add(good);
//            }
//        }
//        BigDecimal totalPrice = BigDecimal.ZERO;
//        for (Good good : goods) {
//            totalPrice =totalPrice.add(good.getPrice());
//        }
//        if (totalPrice.equals(BigDecimal.ZERO)){
//            return ResponseData.fail(ResponseEnum.FAIL);
//        }
//        addProducer.send("add", jsonString);
        List<Good> goods = new ArrayList<>();
        for (Good good : goodList) {
            if (good.getNumber()!=0){
                goods.add(good);
            }
        }
        Order order = new Order();
        //生成雪花id
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        String orderNum = String.valueOf(snowflakeIdGenerator.nextId());
        order.setOrderNum(orderNum);
        //计算总价格
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Good good : goods) {
            totalPrice =totalPrice.add(good.getPrice());
        }
        if (totalPrice.equals(BigDecimal.ZERO)){
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        //获取当前时间
        order.setTotalPrice(totalPrice);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        order.setCreateTime( now.format(formatter));
        order.setUserId(1);
        order.setStatus(1);
//        redisTemplate.opsForHash().put("GOODSERVICEIMPL:ADDORDER",order.getOrderNum()+"",order);
        String jsonString = JSON.toJSONString(order);
        //把订单编号存入redis中
        redisTemplate.opsForValue().set(orderNum,orderNum,5, TimeUnit.MINUTES);
        addProducer.send("add", jsonString);
//        orderMapper.addOrder(order);
        /**
         * 生成详细订单信息
         */
        OrderItem orderItem = new OrderItem();
        for (Good good : goods) {
            orderItem.setGoodId(good.getId());
            orderItem.setGoodName(good.getName());
            orderItem.setPrice(good.getPrice());
            orderItem.setBuyCount(good.getNumber());
            orderItem.setCreateTime(order.getCreateTime());
            orderItem.setOrderId(order.getOrderNum());
            orderItem.setSumPrice(good.getPrice().multiply(new BigDecimal(good.getNumber())));
//            redisTemplate.opsForHash().put("GOODSERVICEIMPL:ADDORDERItem",order.getOrderNum()+"",orderItem);
            String json = JSON.toJSONString(orderItem);
            addProducer1.send("add1", json);
//            orderItemMapper.addOrderItem(orderItem);
        }
        Integer ttl=1000*10;
        delayProducer.sendDelay("delay",order.getOrderNum(),ttl);
        return ResponseData.ok();
    }

    @Override
    public ResponseData<?> queryAllOrder() {
        return ResponseData.ok(orderMapper.queryAllOrder());
    }

    @Override
    public ResponseData<?> goodPayOrder(Order order) {
        return ResponseData.ok(orderMapper.goodPayOrder(order));
    }

    @Override
    public ResponseData<?> handleCancel(Order order) {
        return ResponseData.ok(orderMapper.handleCancel(order));
    }
    @Override
    public ResponseData<?> getOrderItemByOrderId(Order order) {
        return ResponseData.ok(orderItemMapper.getOrderItemByOrderId(order.getOrderNum()));
    }
}
